package com.sanley.piss.web.controller;

import com.gitee.dreamkaylee.shiro.util.SubjectUtil;
import com.sanley.piss.model.exceptionUtil.json.JsonResult;
import com.sanley.piss.model.vo.LoginUser;
import com.sanley.piss.model.vo.UserVO;
import com.sanley.piss.web.service.mapper.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 用户表控制层
 * Create By  on 2021/3/18.
 *
 * @author 张凯大天才
 */
@Api(value = "用户表controller",tags = "用户表操作接口")
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 用户表业务接口
     */
    @Autowired
    private IUserService IUserService;

    /**
     * 插入用户
     *
     * @param user 用户信息
     * @return
     */
    @ApiOperation(value ="插入一条记录" )
    @RequestMapping(value = "/insert",method = RequestMethod.PUT)
    @ApiImplicitParams
    ({@ApiImplicitParam(name = "organization.id",value = "部门ID,从页面获取"),
            @ApiImplicitParam(name = "roles[0].id",value = "角色ID,从页面获取,可填入多个")})
    public JsonResult insert(@Valid UserVO user) {
        IUserService.insert(user);
        return JsonResult.success();
    }

    /**
     * 更改账户状态
     * @param ids 获取的用户id
     * @return
     */
    @ApiOperation(value ="根据ID删除多条记录" )
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.DELETE)
    public JsonResult updateState(@PathVariable("ids") Integer[] ids) {
        IUserService.removeByIds(ids);
        return JsonResult.success();
    }

    /**
     * 修改当前登录账号密码
     *
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return
     */
    @ApiOperation(value ="修改当前登录用户密码" )
    @RequestMapping(value = "/updatePassword",method = RequestMethod.POST)
    public JsonResult updatePassword(String oldPassword, String newPassword) {
        LoginUser loginUser = SubjectUtil.getPrincipal(LoginUser.class);
        IUserService.updatePassword(oldPassword, newPassword, Integer.valueOf(loginUser.getUserId()));
        return JsonResult.success();
    }

    /**
     * 重置密码
     *
     * @param id          获取的用户id
     * @param newPassword 重置后的密码
     * @return
     */
    @RequiresRoles(value = {"superadmin"},logical = Logical.OR)
    @ApiOperation(value ="superadmin重置密码" )
    @RequestMapping(value = "/{id}/roloadPassword",method = RequestMethod.POST)
    public JsonResult roloadPassword(@PathVariable("id") Integer id, String newPassword) {
        IUserService.roloadPassword(newPassword, id);
        return JsonResult.success();
    }


}
